package schema

import (
	"blog/internal/ent/schema/mixin"
	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
)

type BlogsGraph struct {
	ent.Schema
}

func (BlogsGraph) Fields() []ent.Field {
	return []ent.Field{
		field.String("id").Unique().Comment("ID"),
		field.Int("account" +
			"_id").Positive().Comment("所属账户"),
		field.String("title").Default("").Comment("标题"),
		field.String("description").Default("").Comment("描述"),
		field.Int("browse_number").Default(0).Comment("浏览量"),
		field.String("content").NotEmpty().Comment("内容").
			SchemaType(map[string]string{
				dialect.MySQL: "json",
			}),
		field.Int8("is_hidden").Default(0).Comment("是否隐藏:0否,1是").
			SchemaType(map[string]string{
				dialect.MySQL: "tinyint",
			}),
		field.Int("blog_id").Optional().Default(0).Comment("博客ID"),
		field.Int("project_id").Optional().Default(0).Comment("项目ID"),
	}
}

func (BlogsGraph) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixin.Time{},
		mixin.SoftDelete{},
	}
}
func (BlogsGraph) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("blogs", Blogs.Type).Ref("graph").Field("blog_id").Unique(),
		edge.From("projects", UserProject.Type).Ref("graph").Field("project_id").Unique(),
	}
}
func (BlogsGraph) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{Table: "blogs_graph"},
		entsql.WithComments(true),
		schema.Comment("图"),
	}
}
